#include "./memory.h"

void* mem_alloc(unsigned int size) {
  if (size == 0)
    return NULL;
  void* p = malloc(size);
  if (p != NULL) {
    mem_set(p, 0x00, size);
  } else {
    ERROR("memory can not alloc.");
    // exit(error_memAlloc);
  }
  return p;
}

void* mem_set(void* dst, int c, unsigned int n) {
  unsigned char* d = (unsigned char*)dst;

  while (n--) {
    *d++ = (unsigned char)c;
  }
  return dst;
}

void* mem_cpy(void* dst, const void* src, unsigned int n) {
  unsigned char* d = (unsigned char*)dst;
  unsigned char* s = (unsigned char*)src;
  unsigned char* r = d;

  while (n--) {
    *d++ = *s++;
  }
  return r;
}

int mem_cmp(const void* dst, const void* src, unsigned int n) {
  unsigned char* d = (unsigned char*)dst;
  unsigned char* s = (unsigned char*)src;

  while (--n) {
    if ((*d == *s)) {
      d++;
      s++;
    } else {
      break;
    }
  }
  return *d - *s;
}
